package VISTA;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.SwingUtilities;

import MODELO.ModeloIngrediente;
import MODELO.ModeloProducto;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class frmOrdenes extends javax.swing.JFrame {
	private JPanel pnlOrdenes;
	private JLabel jLabel2;
	private JLabel jLabel1;
	private JComboBox cmbCantidad;
	private JLabel fecha;
	private JTable tblProductoOrden;
	private JScrollPane scpCatalogo;
	private JPanel pnlLlenarOrden;
	private JTextField txtEscribirNombres;
	private JTable tblProductoOrdenes;
	private JScrollPane scpProductoOrdenes;
	private JButton btnCancelarOrden;
	private JButton btnOkOrden;
	private JLabel lblBsTotal;
	private JLabel lblTotalMonto;
	private JLabel lblTotal;
	private JLabel lblMontoImpuesto;
	private JButton btnAgregar;
	private JButton btnRetirar;
	private JLabel lblNombres;
	private JTextField txtCedula;
	private JLabel lblCedula;
	private JLabel lblNro;
	private JLabel lblNroOrden;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				frmOrdenes inst = new frmOrdenes();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public frmOrdenes() {
		super("Registro de Ordenes");
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				pnlOrdenes = new JPanel();
				getContentPane().add(pnlOrdenes, BorderLayout.CENTER);
				pnlOrdenes.setLayout(null);
				pnlOrdenes.setBackground(new java.awt.Color(255,255,255));
				pnlOrdenes.setPreferredSize(new java.awt.Dimension(1210, 516));
				{
					lblNroOrden = new JLabel();
					pnlOrdenes.add(lblNroOrden);
					lblNroOrden.setText("Nro de Orden:");
					lblNroOrden.setBounds(21, 7, 118, 24);
				}
				{
					lblNro = new JLabel();
					pnlOrdenes.add(lblNro);
					lblNro.setBounds(145, 12, 130, 19);
					lblNro.setBackground(new java.awt.Color(229,229,229));
				}
				{
					lblCedula = new JLabel();
					pnlOrdenes.add(lblCedula);
					lblCedula.setText("Cedula:");
					lblCedula.setBounds(21, 72, 99, 30);
				}
				{
					txtCedula = new JTextField();
					pnlOrdenes.add(txtCedula);
					txtCedula.setBounds(157, 79, 171, 23);
				}
				{
					lblNombres = new JLabel();
					pnlOrdenes.add(lblNombres);
					lblNombres.setText("Nombres:");
					lblNombres.setBounds(21, 117, 107, 24);
				}
				{
					txtEscribirNombres = new JTextField();
					pnlOrdenes.add(txtEscribirNombres);
					txtEscribirNombres.setBounds(157, 119, 284, 22);
				}
				{
					
					pnlLlenarOrden = new JPanel();
					pnlLlenarOrden.setBorder(BorderFactory.createTitledBorder("Carga de Producto"));
					pnlOrdenes.add(pnlLlenarOrden);
					pnlLlenarOrden.setLayout(null);
					pnlLlenarOrden.setBounds(21, 165, 1169, 294);
					pnlLlenarOrden.setBackground(new java.awt.Color(228,196,116));
					{
						scpCatalogo = new JScrollPane();
						pnlLlenarOrden.add(scpCatalogo);
						scpCatalogo.setBounds(661, 50, 492, 151);
						scpCatalogo.setBackground(new java.awt.Color(255,255,255));
						{
							TableModel tblProductoOrdenModel = 
								new DefaultTableModel(
										new String[][] { { "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" } },
										new String[] { "Cantidad", "Descripcion", "Monto" });
							tblProductoOrden = new JTable();
							scpCatalogo.setViewportView(tblProductoOrden);
							tblProductoOrden.setModel(tblProductoOrdenModel);
							tblProductoOrden.setBounds(17, 28, 682, 138);
							tblProductoOrden.setPreferredSize(new java.awt.Dimension(472, 135));
						}
					}
					{
						btnRetirar = new JButton();
						pnlLlenarOrden.add(btnRetirar);
						btnRetirar.setText("Retirar");
						btnRetirar.setBounds(535, 121, 102, 31);
						btnRetirar.setActionCommand("Retirar");
					}
					{
						btnAgregar = new JButton();
						pnlLlenarOrden.add(btnAgregar);
						btnAgregar.setText("Agregar");
						btnAgregar.setBounds(535, 157, 102, 31);
						btnAgregar.setActionCommand("Agregar");
					}
					{
						lblMontoImpuesto = new JLabel();
						pnlLlenarOrden.add(lblMontoImpuesto);
						lblMontoImpuesto.setBounds(973, 226, 123, 25);
					}
					{
						lblTotal = new JLabel();
						pnlLlenarOrden.add(lblTotal);
						lblTotal.setText("Total:");
						lblTotal.setBounds(870, 263, 91, 23);
					}
					{
						lblTotalMonto = new JLabel();
						pnlLlenarOrden.add(lblTotalMonto);
						lblTotalMonto.setBounds(973, 263, 123, 23);
					}
					{
						lblBsTotal = new JLabel();
						pnlLlenarOrden.add(lblBsTotal);
						lblBsTotal.setText("Bs");
						lblBsTotal.setBounds(1108, 263, 36, 23);
					}
					{
						scpProductoOrdenes = new JScrollPane();
						pnlLlenarOrden.add(scpProductoOrdenes);
						scpProductoOrdenes.setBounds(17, 50, 492, 151);
						{
							TableModel tblScrollProductoOrdenesModel = 
								new DefaultTableModel(
										new String[][] { { "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" },{ "", "" }, { "", "" } },
										new String[] { "Codigo", "Descripcion", "Precio Unitario" });
							tblProductoOrdenes = new JTable();
							scpProductoOrdenes.setViewportView(tblProductoOrdenes);
							tblProductoOrdenes.setModel(tblScrollProductoOrdenesModel);
							tblProductoOrdenes.setPreferredSize(new java.awt.Dimension(489, 144));
						}
					}
					{
						ComboBoxModel cmbCantidadModel = 
							new DefaultComboBoxModel(
									new String[] { "Item One", "Item Two" });
						cmbCantidad = new JComboBox();
						pnlLlenarOrden.add(cmbCantidad);
						
						cmbCantidad.setBounds(535, 73, 102, 31);
						int i =1;
						while(i<=100){
							cmbCantidad.addItem(i);
							i++;
						}
					}
					{
						jLabel1 = new JLabel();
						pnlLlenarOrden.add(jLabel1);
						jLabel1.setText("Cantidad");
						jLabel1.setBounds(558, 43, 65, 24);
					}
				}
				{
					btnOkOrden = new JButton();
					pnlOrdenes.add(btnOkOrden);
					btnOkOrden.setText("OK");
					btnOkOrden.setBounds(956, 480, 117, 31);
					btnOkOrden.setActionCommand("OK");
				}
				{
					btnCancelarOrden = new JButton();
					pnlOrdenes.add(btnCancelarOrden);
					btnCancelarOrden.setText("Cancelar");
					btnCancelarOrden.setBounds(1089, 480, 101, 31);
					btnCancelarOrden.setActionCommand("Cancelar");
				}			
				{
					fecha = new JLabel();
					pnlOrdenes.add(fecha);
					fecha.setBounds(157, 37, 171, 21);
				}
				{
					jLabel2 = new JLabel();
					pnlOrdenes.add(jLabel2);
					jLabel2.setText("Fecha:");
					jLabel2.setBounds(22, 37, 118, 24);
				}
			}
			pack();
			this.setSize(1220, 606);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

	public JLabel getLblTotalMonto() {
		return lblTotalMonto;
	}

	public void setLblTotalMonto(JLabel lblTotalMonto) {
		this.lblTotalMonto = lblTotalMonto;
	}

	public JTable getTblProductoOrden() {
		return tblProductoOrden;
	}

	public void setTblProductoOrden(JTable tblProductoOrden) {
		this.tblProductoOrden = tblProductoOrden;
	}

	public JTextField getTxtEscribirNombres() {
		return txtEscribirNombres;
	}

	public void setTxtEscribirNombres(JTextField txtEscribirNombres) {
		this.txtEscribirNombres = txtEscribirNombres;
	}

	public JTable getTblProductoOrdenes() {
		return tblProductoOrdenes;
	}

	public void setTblProductoOrdenes(JTable tblProductoOrdenes) {
		this.tblProductoOrdenes = tblProductoOrdenes;
	}


	

	public JTextField getTxtCedula() {
		return txtCedula;
	}

	public void setTxtCedula(JTextField txtCedula) {
		this.txtCedula = txtCedula;
	}
	
	public Vector<String []> getInfoTabla(){
        Vector<String []> v = new Vector<String[]>();
        for (int i = 0; i < tblProductoOrden.getRowCount(); i++){
        	if(!tblProductoOrden.getValueAt(i, 0).equals(""))
            {String [] s = new String [] {""+tblProductoOrden.getValueAt(i, 0),""+tblProductoOrden.getValueAt(i, 1),""+tblProductoOrden.getValueAt(i, 2)};
            v.add(s);}
        	else
        		break;
         }
         return v;
  }
	
	public void addListener(ActionListener actionListener){
		this.btnAgregar.addActionListener(actionListener);
		this.btnCancelarOrden.addActionListener(actionListener);
		this.btnOkOrden.addActionListener(actionListener);
		this.btnRetirar.addActionListener(actionListener);
	}
	
	public void limpiar(int num) {
		txtCedula.setText("");
		txtEscribirNombres.setText("");
		lblTotalMonto.setText("");
		lblNro.setText(""+num);
		cmbCantidad.setSelectedIndex(0);
		Date d = new Date();
		fecha.setText(""+d.toLocaleString());
		TableModel m = tblProductoOrden.getModel();	
		int i =0;
		while(i< tblProductoOrden.getRowCount()) {
			m.setValueAt("", i, 0);
			m.setValueAt("", i, 1);
			m.setValueAt("", i, 2);
			i++;
		}
	}
	
	public void mostrarMensaje(String mensaje) {
		JOptionPane.showMessageDialog(this, mensaje);
	}



	public void quitar() {
		int i = tblProductoOrden.getSelectedRow();
		if(i!=-1){
			tblProductoOrden.setValueAt("", i, 1);
			tblProductoOrden.setValueAt("", i, 0);
			tblProductoOrden.setValueAt("", i, 2);
			calcularTotal();
		}
		else
			mostrarMensaje("Selecione una fila");
	}

	public void agregar(ModeloProducto m) {
		int i =0;
		while(i< tblProductoOrden.getRowCount()) {
			if(tblProductoOrden.getValueAt(i,0).equals("")){
				tblProductoOrden.setValueAt(m.getDescripcion(), i, 1);
				tblProductoOrden.setValueAt(cmbCantidad.getItemAt(cmbCantidad.getSelectedIndex()), i, 0);
				double precio = m.getPrecio() *(Integer)cmbCantidad.getItemAt(cmbCantidad.getSelectedIndex()); 
				tblProductoOrden.setValueAt(precio, i, 2);
				break;
			}
			else if(tblProductoOrden.getValueAt(i,1).equals(m.getDescripcion()))
				{
				double precio = m.getPrecio() *(Integer)cmbCantidad.getItemAt(cmbCantidad.getSelectedIndex()); 
				tblProductoOrden.setValueAt(precio, i, 2);
				break;
				}
			i++;
		}
		calcularTotal();
	}
	
	public void calcularTotal() {
		int i =0;
		double total =0;
		while(i< tblProductoOrden.getRowCount()) {
			if(tblProductoOrden.getValueAt(i,0).equals("")){
				break;
			}
			else
				total+=(Double)tblProductoOrden.getValueAt(i, 2);
			i++;
		}
		lblTotalMonto.setText(""+total);
	}

	
	
	public boolean contar() {
		int i =0;
		int cont = 0;
		while(i< tblProductoOrden.getRowCount()) {
			if(tblProductoOrden.getValueAt(i,0).equals("")){
				cont++;
			}
			i++;
		}
		return (cont==tblProductoOrden.getRowCount());
	}

	

	public JLabel getLblNro() {
		return lblNro;
	}

	public void setLblNro(JLabel lblNro) {
		this.lblNro = lblNro;
	}

	public Integer cantidad() {
		return (Integer)cmbCantidad.getItemAt(cmbCantidad.getSelectedIndex());
	}

	public void setCmbCantidad(JComboBox cmbCantidad) {
		this.cmbCantidad = cmbCantidad;
	}
	
	

}
